package com.web.Controller;

import java.io.UnsupportedEncodingException;

import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.crypto.hash.Md5Hash;

import com.web.common.JsonResult;
import com.web.config.RegVal;
import com.web.model.*;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;


public class RegisterController extends Controller{
	private static Logger logger = Logger.getLogger(RegisterController.class);
	
	public void index() {
		
		String errstr = getPara("errstr" , "请认真填写").trim();
		setAttr("errstr", errstr);
		
	}

	public void doregister() {
		
		String username = getPara("username" , "0").trim();
		String password  = getPara("password" , "0").trim();
		String card = getPara("card" , "0");
		String real_name =getPara("real_name" ,"0").trim();
		String short_name = getPara("short_name" , "0");
		String top =getPara("top" ,null);
		String sex =getPara("sex" ,"");
		int high =getParaToInt("high" ,0);
		int weight =getParaToInt("weight" ,0);
		String education =getPara("education" ,"0");
		String hometown =getPara("hometown" ,"0");
		String work =getPara("work" ,"0");
		String workaddr =getPara("workaddr" ,"0");
		String myself =getPara("myself" ,"0");
		String birthday =getPara("birthday" ,"0");
		String others =getPara("others" ,"0");
		int push =getParaToInt("push" ,0);
		String fang =getPara("fang" ,"0");
		String che =getPara("che" ,"0");
		String errstr = "";
		card=card.toUpperCase();
		
		Long i =Db.queryLong("select count(*) from users where  username=?",username);
		logger.info(username);
		
		if(real_name == ""){
			errstr ="姓名不能为空";
		}
		if(!RegVal.isIDCard(card)){
			errstr ="身份证输入有误";
		}
		if(!RegVal.isPassword(password)){
			errstr ="密码需为6--16位";
		}
		if(i >0){
			errstr ="该手机号已被注册";
		}
		if(!RegVal.isMobile(username)){
			errstr ="手机号输入有误";
			
		}
		
		
		if(errstr.isEmpty()){
			PropKit.use("jfinal.properties");
			String salt=PropKit.get("salt");
			password= new Md5Hash(password, salt).toString();
			UsersModel u=new UsersModel();
			u.set("username", username)
			.set("password", password)
			.set("card", card)
			.set("real_name", real_name)
			.set("short_name", short_name)
			.set("sex", sex).set("birthday", birthday)
			.set("high", high).set("weight", weight)
			.set("education", education).set("hometown", hometown)
			.set("work", work).set("workaddr", workaddr)
			.set("myself", myself).set("others", others)
			.set("push", push).set("fang", fang)
			.set("che", che).set("top", top).save();
			int id = u.getInt("id");
			errstr=Integer.toString(id);
			setAttr("id", id);
			Record user_roles = new Record().set("user_id", id).set("role_id", 2);
			Db.save("user_roles", user_roles);
			
			renderJson(new JsonResult(JsonResult.SUCCESS, errstr));
			return;
		}else{
			renderJson(new JsonResult(JsonResult.ERROR, errstr));
			return;
		}
		
	}
	public void success() {
		
		String id = getPara("id","0").trim();
		setAttr("id", id);
		
	}
}
